package com.wdl.modules.myhouse.controller;


import com.wdl.modules.myhouse.dto.FileTypeDTO;
import com.wdl.modules.myhouse.entity.FileTypeEntity;
import com.wdl.modules.myhouse.service.FileTypeService;
import com.wdl.modules.myhouse.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.crypto.spec.PSource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 昏昏的君
 */
@RestController
@RequestMapping("/fileType")
@Api(tags = "文件类型管理")
public class FileTypeController {

    @Autowired
    private FileTypeService fileTypeService;


    @GetMapping("/source2Name/{sourceName}")
    @ApiOperation(value = "根据分类获取分类名")
    public Result<String> getTypeName(@PathVariable("sourceName")String sourceName){
        Map<String, String> souceName2Name = fileTypeService.list().stream().collect(Collectors.toMap(FileTypeEntity::getSourceTypeName, FileTypeEntity::getTypeName));
        return new Result<String>().setData(souceName2Name.get(sourceName)==null?"未知标签":souceName2Name.get(sourceName));
    }

    @GetMapping("/root")
    @ApiOperation(value = "获取所有根节点")
    public Result<List<FileTypeDTO>> getAllRoot(){
       List<FileTypeDTO> fileTypeDTOList= fileTypeService.getAllRoot();
       return new Result<List<FileTypeDTO>>().ok(fileTypeDTOList);
    }


    @PostMapping("/add")
    @ApiOperation(value = "添加一个文件类型")
    public Result<FileTypeDTO> add(@RequestBody FileTypeDTO dto){
        FileTypeEntity entity = new FileTypeEntity();
        BeanUtils.copyProperties(dto,entity);
        boolean save = fileTypeService.save(entity);
        dto.setId(entity.getId());
        if (save){
            return new Result<FileTypeDTO>().ok(dto);
        }else{
            return new Result<FileTypeDTO>().error();
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "更新一个文件类型")
    public Result<Boolean> update(@RequestBody FileTypeDTO dto){
        FileTypeEntity entity = new FileTypeEntity();
        BeanUtils.copyProperties(dto,entity);
        //构建childTypeId
        List<Integer> ids = dto.getChildNodes().stream().map(node -> node.getId()).filter(id->id!=null).collect(Collectors.toList());
        StringBuilder builder = new StringBuilder();
        for(int i=0;i<ids.size();i++){
            if (i!=ids.size()-1){
                builder.append(ids.get(i) +",");
            }else{
                builder.append(ids.get(i));
            }
        }
        entity.setChildTypeId(builder.toString());
        boolean save = fileTypeService.updateById(entity);
        if (save){
            return new Result<Boolean>().ok(true);
        }else{
            return new Result<Boolean>().error();
        }
    }

    @GetMapping("/del/{id}")
    public Result<Boolean> delById(@PathVariable("id")String id){
        boolean b = fileTypeService.removeById(id);
        if (b){
            return new Result<Boolean>().ok();
        }else{
            return new Result<Boolean>().error();
        }
    }


}
